package com.jetgege.ipctest;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private IBookManager mRemoteBookManager;

    private static final int  HANDLER_CODE =1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intentService = new Intent(this,BookManagerService.class);
        bindService(intentService,serviceConnection,BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {

        if (mRemoteBookManager!=null&&mRemoteBookManager.asBinder().isBinderAlive()){
            try {
                mRemoteBookManager.unregisterListener(mOnNewBookArrived);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        unbindService(serviceConnection);
        super.onDestroy();
    }


    private IOnNewBookArrivedListener mOnNewBookArrived = new IOnNewBookArrivedListener.Stub() {
        @Override
        public void onNewBookArrived(Book newBook) throws RemoteException {
            mHandler.obtainMessage(HANDLER_CODE,newBook).sendToTarget();
        }
    };

    private  Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case HANDLER_CODE:
                    Log.e(TAG,"Receive Book "+msg.obj.toString());
                    break;
                    default: super.handleMessage(msg);
                    break;
            }

        }
    };

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.e(TAG,"onServiceConnected");
            IBookManager bookManager = IBookManager.Stub.asInterface(iBinder);
            mRemoteBookManager=bookManager;
            if (bookManager!=null){
                try {
                    bookManager.addBook(new Book(3,"星辰大海"));
                    List<Book> bookList = bookManager.getBookList();
                    for (int i=0;i<bookList.size();i++){
                        Log.e(TAG,bookList.get(i).toString());
                    }
                    bookManager.registerListener(mOnNewBookArrived);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e(TAG,"onServiceDisconnected");
        }
    };
}
